package com.supinfo.food.tools;
import android.util.Log;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

/**
 * @desc Classe de serialisation
 * 
 * @projectName : Launcher
 * @author Keitaro
 * 
 */
public class SerialisationTools {

	
	static public String serialisation(Object obj) {
		String out = null;
		if (obj != null) {
			try {
				ByteArrayOutputStream baos = new ByteArrayOutputStream();
				ObjectOutputStream oos = new ObjectOutputStream(baos);
				oos.writeObject(obj);
				out = Base64.encodeBytes(baos.toByteArray());
			} catch (IOException e) {
				Log.e("","",e);
				return null;
			}
		}
		return out;
	}

	static public Object deserialisation(String str) {
		Object out = null;
		if (str != null) {
			try {
				ByteArrayInputStream bios = new ByteArrayInputStream(
						Base64.decode(str));
				ObjectInputStream ois = new ObjectInputStream(bios);
				out = ois.readObject();
			} catch (IOException e) {
				Log.e("","",e);
				return null;
			} catch (ClassNotFoundException e) {
				Log.e("","",e);
				return null;
			}
		}
		return out;
	}
}
